// pages/property-repair-detail/property-repair-detail.js
import api from "../../utils/request"
Page({
  data: {
    id: "",
    statusClass: "pending",
    currentUserId: "1003", // 当前登录用户ID
    detail: {
      id: 3,
      repairNumber: "WR1749656132502",
      repairTime: "2025-06-04",
      houseId: 117,
      houseAddress: "城市花园2栋2单元3层302",
      repairArea: "卧室",
      repairTypeId: 1,
      repairTypeName: "家电维修",
      problemDescription: "卧室空调制冷失效，室内温度过高，需要专业维修人员检查制冷系统。",
      status: 1,
      statusText: "待接单",
      staffId: 1003,
      staffName: "李师傅",
      staffPhone: "138****5678",
      lastAcceptTime: null,
      canAccept: true,
      isAccepted: false,
    },
    steps: [
      {
        text: "待派单",
        desc: "工单已提交",
      },
      {
        text: "待接单",
        desc: "维修人员已分配",
      },
      {
        text: "已完成",
        desc: "维修已完成",
      },
    ],
    operationLogs: [
      {
        id: 1,
        time: "2025-06-04 14:30",
        action: "工单已创建",
        operator: "系统",
      },
      {
        id: 2,
        time: "2025-06-04 15:00",
        action: "已派单给李师傅",
        operator: "物业管理员",
      },
    ],
  },

  onLoad(options) {
    if (options.id) {
      this.setData({
        id: options.id,
      })
      this.fetchDetail(options.id)
    }
  },

  // 获取详情数据
  fetchDetail(id) {
    wx.showLoading({
      title: "加载中...",
    })

    // 模拟API请求
    setTimeout(async() => {
      // 实际项目中这里应该是API请求
      const result = await api.get(`/property/repairRequest/${id}`)
      this.setData({
        detail: result.data,
        operationLogs: result.logs,
        statusClass: this.getStatusClass(result.data.status)
      })

      this.setData({
        statusClass: this.getStatusClass(this.data.detail.status),
      })

      wx.hideLoading()
    }, 500)
  },

  // 获取状态样式类
  getStatusClass(status) {
    const statusMap = {
      0: "pending",
      1: "processing",
      2: "completed",
    }
    return statusMap[status] || "pending"
  },

  // // 派单
  // onAssignOrder() {
  //   wx.navigateTo({
  //     url: `/pages/assign-order/assign-order?id=${this.data.id}&repairNumber=${this.data.detail.repairNumber}`,
  //   })
  // },

  // // 重新派单
  // onReassignOrder() {
  //   wx.showModal({
  //     title: "确认重新派单",
  //     content: "确定要重新分配维修人员吗？",
  //     success: (res) => {
  //       if (res.confirm) {
  //         wx.navigateTo({
  //           url: `/pages/assign-order/assign-order?id=${this.data.id}&repairNumber=${this.data.detail.repairNumber}&reassign=true`,
  //         })
  //       }
  //     },
  //   })
  // },

  // 接单
  onAcceptOrder() {
    wx.showModal({
      title: "确认接单",
      content: `确定要接受工单 ${this.data.detail.repairNumber} 吗？`,
      success: (res) => {
        if (res.confirm) {
          this.handleAcceptOrder()
        }
      },
    })
  },

  // 处理接单
  handleAcceptOrder() {
    wx.showLoading({
      title: "接单中...",
    })

    // 模拟API请求
    setTimeout(async() => {
      // 实际项目中这里应该是API请求
      await api.put(`/property/repairRequest/accept/${this.data.id}`)

      const currentTime = new Date().toISOString().split("T")[0]
      this.setData({
        "detail.lastAcceptTime": currentTime,
        "detail.isAccepted": true,
        "detail.canAccept": false,
      })

      // 添加操作记录
      const newLog = {
        id: Date.now(),
        time: new Date().toLocaleString(),
        action: "维修人员已接单",
        operator: this.data.detail.staffName,
      }

      wx.hideLoading()
      wx.showToast({
        title: "接单成功",
        icon: "success",
      })
    }, 1000)
    wx.navigateTo({ url: "/pages/repair/repair" })
  },

  // 完成工单
  onCompleteOrder() {
    wx.showModal({
      title: "确认完成",
      content: `确定要完成工单 ${this.data.detail.repairNumber} 吗？`,
      success: (res) => {
        if (res.confirm) {
          this.handleCompleteOrder()
        }
      },
    })
  },

  // 处理完成工单
  handleCompleteOrder() {
    wx.showLoading({
      title: "处理中...",
    })

    // 模拟API请求
    setTimeout(() => {
      // 实际项目中这里应该是API请求
      // await api.completeRepairOrder(this.data.id)

      this.setData({
        "detail.status": 2,
        "detail.statusText": "已完成",
        statusClass: "completed",
      })

      // 添加操作记录
      const newLog = {
        id: Date.now(),
        time: new Date().toLocaleString(),
        action: "工单已完成",
        operator: this.data.detail.staffName,
      }

      wx.navigateTo({ url: "/pages/repair/repair" })
  })
},


  // 查看评价
  onViewFeedback() {
    wx.navigateTo({
      url: `/pages/repair-feedback/repair-feedback?id=${this.data.id}`,
    })
  },
})
